﻿using System;
using System.Collections.Generic;
using System.Linq;
using StockCrawlerLib.DAL;
using StockCrawlerLib;
using CrawlerController;
using System.IO;
using OfficeOpenXml;
using System.Data;

namespace StockCrawler
{
    public partial class UpdateHistory : System.Web.UI.Page
    {
        private HistoryRepository _historyRepository = new HistoryRepository();
        private StockRepository _stockRepository = new StockRepository();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                dateStart.Date = DateTime.Now;
                dateEnd.Date = DateTime.Now;
                gridHistory.DataBind();
            }
        }

        private void FptsImport()
        {
            BaseCrawler objCrawler = new FptsCrawler2();
            DateTime dt = dateStart.Date;
            while (dt <= dateEnd.Date)
            {
                if (dt.DayOfWeek != DayOfWeek.Saturday 
                    && dt.DayOfWeek != DayOfWeek.Sunday)
                {
                    //Lấy dữ liệu price
                    objCrawler.ImportPriceHisotry("", dt);

                    //Lấy dữ liệu đặt lệnh
                    objCrawler.ImportOrderHisotry("", dt);

                    //Lấy dữ liệu giao dịch nước ngoài
                    objCrawler.ImportForeignHistory(dt);
                    objCrawler.ImportForeign2History(dt);
                }
                dt = dt.AddDays(1);
            }
            gridHistory.DataBind();
            //gridViewLog.ItemsSource = SystemConfig.SystemLog;
        }
        protected void btnUpdateHistory_Click(object sender, EventArgs e)
        {
            FptsImport();
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            gridHistory.DataBind();
        }

        protected void LinqServerModeDataSourceHistory_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
        {
            e.KeyExpression = "id_stock";
            e.QueryableSource = _historyRepository.SelectByNgayGiaoDich(dateStart.Date);
        }

        protected void btnUpdataLoaiCP_Click(Object sender, EventArgs e)
        {
            StockDataClassesDataContext dc = new StockDataClassesDataContext();
            if (FileUpload1.HasFile && Path.GetExtension(FileUpload1.FileName) == ".xlsx")
            {
                using (var excel = new ExcelPackage(FileUpload1.PostedFile.InputStream))
                {
                    int count = 0;
                    var ws = excel.Workbook.Worksheets.First();
                    var hasHeader = false;  // adjust accordingly
                                           // add DataColumns to DataTable

                    // add DataRows to DataTable
                    int startRow = hasHeader ? 2 : 1;
                    for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
                    {
                        var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
                        foreach (var code in wsRow)
                        {
                            stock obj = _stockRepository.Select(code.Text);
                            if (obj != null)
                            {
                                obj.loaicp = (bool)cbLoaiCP.SelectedItem.Value;
                                if (obj != null)
                                    _stockRepository.Update(obj);
                                count++;
                            }
                        }
                            
                    }
                    var msg = String.Format("Đã cập nhật thành công {0}/{1} mã", count, ws.Dimension.End.Row);
                    UploadStatusLabel.Text = msg;
                }
            }
            else
            {
                UploadStatusLabel.Text = "You did not specify a file to upload.";
            }
        }
    }
}